<?php    
	require('../Services/util/ConfigUtil.php');
	require('../Services/util/ThumbHandler.php');
	
	$_db = null;
 	$sql = null;
 	$result = null;
 	$thumbHandler = new ThumbHandler();
 	
 	try
	{ 	
		$_db = mysql_connect( ConfigUtil::SERVER_NAME(),
							  ConfigUtil::MYSQL_USERNAME(),
							  ConfigUtil::MYSQL_PASSWORD());
		mysql_select_db(ConfigUtil::MYSQL_TABLENAME(),$_db);
		mysql_query("SET NAMES 'utf8'");
	}catch(Excption $exc)
	{
		return $exc;
	}
	
	$fileUploader = $_REQUEST["username"];
	
	if($fileUploader)
	{
		$email = strtolower(trim($fileUploader));
 		try
 		{
 			$sql = "SELECT user.username FROM user WHERE user.username='$email'";
 			$result = mysql_query($sql,$_db);
 			$numRows = mysql_num_rows($result);
	 		if($numRows>=1)
	 		{
	 			$ano = $_REQUEST["category"];
	 			$fileCatagory = NULL;
	 			switch($ano)
	 			{
	 				case "image":
	 					$fileCatagory = "images/";
	 					break;
	 				case "pdf":
	 					$fileCatagory = "pdfs/";
	 					break;
	 				case "swf":
	 					$fileCatagory = "swfs/";
	 					break;
	 				case "testcode":
	 					$fileCatagory = "testcodes/";
	 					break;	
	 				default:
	 					break;					
	 			}
				$fileName = $_FILES["Filedata"]["name"]; 
				$fileNameNew = strpbrk($fileName, '.');
				$dest=date("Ymd")."_".date("ahis").$fileNameNew;
				$file = $_FILES["Filedata"]["tmp_name"];    
				$path = "uploadFiles/";
				if (move_uploaded_file($file, $path.$fileCatagory.$dest))
				{	
					echo $dest;
					if(file_exists($path.$fileCatagory.$dest))
					{
						$thumbHandler->setSrcImg($path.$fileCatagory.$dest);
				    	$thumbHandler->setCutType(1);//
				    	$thumbnail = $fileCatagory."tn/";
				   		$thumbHandler->setDstImg($path.$thumbnail.$dest);
				    	$thumbHandler->createImg(32,24);
				    	
				    	$newThumbnail = $path.$thumbnail.$dest;
					}else
					{
						return "<fail>thumbnail</fail>";
					}
					
				}else
				{    
					return "<fail>upload</fail>";
				}  
	 		}else
	 		{
	 			return "<error>Your wanna upload but your email not exist in our database!</error>";
	 		}
 		}catch(Exception $exc)
 		{
 			return $exc;
 		}
	}
?>  